package com.bjgov.scoresys.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bjgov.scoresys.entity.ScoreItem;
import com.bjgov.scoresys.entity.ScoreTree;
import com.bjgov.scoresys.form.EditScoreSubItemsForm;
import com.bjgov.scoresys.service.ScoreSubItemsHandler;
import com.bjgov.scoresys.service.ScoreTreeHandler;
import com.bjgov.scoresys.util.RequestUtil;

public class EditScoreSubItemsAction  extends MappingDispatchAction{
	
	
	/**
	 * 修改评分子项
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editScoreItems(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		
		EditScoreSubItemsForm itemsForm = (EditScoreSubItemsForm)form;
		int pid = itemsForm.getPid();
		String[] itemName = itemsForm.getItemname();
		String[] itemDesc = itemsForm.getItemdesc();
		int[] itemID = itemsForm.getId();
		
		if(itemID.length==0)
		{
			request.setAttribute("message", "评分子项名称不能为空！");
			request.setAttribute("returnURL", "editScoreSubItems.jsp?pid="+pid);
			return mapping.findForward("fail");
		}
		
//		ScoreTree scoreTree = ScoreTreeHandler.loadById(pid);
//		//加入区别质量行为和质量实体字段 id 1,40,44-质量行为(Quality_Type=1)  3-质量实体(Quality_Type=2)
//		//根据ScoreTree实体deep_path来判断
//		short qualityType=1;
//		String[] deep_path = scoreTree.getDeepPath().split(",");
//		if(Short.parseShort(deep_path[0])==3)
//		{
//			qualityType=2;
//		}
		for(int i=0;i<itemID.length;i++)
		{
			ScoreItem item = ScoreSubItemsHandler.loadByID(itemID[i]);
			item.setItemName(itemName[i]);
			item.setItemDesc(itemDesc[i]);
			//item.setQualityType(qualityType);
			ScoreSubItemsHandler.save(item);
		}
		
		request.setAttribute("message", "修改成功！");
		request.setAttribute("nextURL", "/system/manageScoreSubItems.jsp");
		return mapping.findForward("tip");
	}
	
	public ActionForward editScoreUrlDesc(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception {
		int id = RequestUtil.getIntParameter(request, "id", 0);
		int pid = RequestUtil.getIntParameter(request, "pid", 0);
		String content = RequestUtil.getStringParameter(request, "content", "");
		if(id<=0) {
			request.setAttribute("message", "评分子项不存在");
			request.setAttribute("returnURL", "/system/editScoreSubItems.jsp?pid=" + pid);
			return mapping.findForward("fail");
		}
		
		ScoreItem item = ScoreSubItemsHandler.loadByID(id);
		item.setItemUrlDesc(content);
		ScoreSubItemsHandler.save(item);
		
		request.setAttribute("message", "修改成功！");
		request.setAttribute("nextURL", mapping.getInput() + "?id=" + id);
		return mapping.findForward("tip");
	}

}
